package com.akeil.flash.preferences;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

import com.akeil.flash.dialog.ChangePasswordDialog;
import com.akeil.flash.utils.SecurityUtils;

import flash.Activator;

public class SecurityPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	private Text userName; // 用户名
	private Text password; // 密码框

	public SecurityPreferencePage() {
		super();
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("管理员账号");
	}

	@Override
	public void init(IWorkbench workbench) {

	}

	// 该方法为必须实现的方法，在此方法中创建页面上的各种控件
	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		// 获取保存此页面的PreferenceStore对象
		final IPreferenceStore preferenceStore = getPreferenceStore();

		new Label(composite, SWT.LEFT).setText("登录用户名:");
		userName = new Text(composite, SWT.BORDER);
		userName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// 设置用户名为保存在文件中的值
		userName.setText(preferenceStore.getString(IPreferenceConstants.SECURITY_USERNAME));

		new Label(composite, SWT.LEFT).setText("登录密码:");
		password = new Text(composite, SWT.BORDER);
		password.setEchoChar('*'); // 设置密码用*显示
		password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// 设置密码为保存在文件中的值
		password.setText(preferenceStore.getString(IPreferenceConstants.SECURITY_PASSWORD));
		new Label(composite, SWT.NONE);

		Button button = new Button(composite, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog(
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
				changePasswordDialog.setUsername(preferenceStore.getString(IPreferenceConstants.SECURITY_USERNAME));
				
				if (IDialogConstants.OK_ID == changePasswordDialog.open()) {
					String username = changePasswordDialog.getUsername();
					String password = changePasswordDialog.getPassword();
					String newpassword = changePasswordDialog.getNewpassword();
					String newpasswordagain = changePasswordDialog.getNewpasswordagain();
					
					if(preferenceStore.getString(IPreferenceConstants.SECURITY_USERNAME).equals(username) 
							&& preferenceStore.getString(IPreferenceConstants.SECURITY_PASSWORD).equals(password)){
						preferenceStore.setValue(IPreferenceConstants.SECURITY_PASSWORD,newpassword);
						
					}else{
						MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "提示", "输入的用户名或密码错误！密码未修改。");
					}
				}
			}
		});
		button.setText("修改密码");
		return composite;
	}

	/*
	 * 覆盖父类中的方法，但单击“恢复默认值”按钮时调用该方法
	 */
	protected void performDefaults() {
		IPreferenceStore preferenceStore = getPreferenceStore();
		
		int ret = SecurityUtils.CheckAuthentication();
		if(0 == ret){
			userName.setText(preferenceStore.getDefaultString(IPreferenceConstants.SECURITY_USERNAME));
			password.setText(preferenceStore.getDefaultString(IPreferenceConstants.SECURITY_PASSWORD));
		}else if(-1 == ret){
			MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "错误", "用户名或密码错误！");
			return;
		}else{
			return;
		}
	}

	/*
	 * 覆盖父类中的方法，但单击“应用”按钮时调用该方法
	 */
	public boolean performOk() {
		IPreferenceStore preferenceStore = getPreferenceStore();
		if (userName != null)
			preferenceStore.setValue(IPreferenceConstants.SECURITY_USERNAME, userName.getText());
		if (password != null)
			preferenceStore.setValue(IPreferenceConstants.SECURITY_PASSWORD, password.getText());
		return true;
	}

	@Override // 用于扩展自己的按钮
	protected void contributeButtons(Composite parent) {
		((GridLayout) parent.getLayout()).numColumns++;
		((GridLayout) parent.getLayout()).numColumns++;
	}
}